package com.jielin.handling.service.mobileapp.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jielin.handling.entity.mobileapp.MessageNotification;
import com.jielin.handling.entity.mobileapp.MessageUser;
import com.jielin.handling.entity.tsuspect.TSuspec;
import com.jielin.handling.entity.vo.CaseVo;
import com.jielin.handling.entity.vo.TSuspecListVo;
import com.jielin.handling.entity.vo.mobileapp.MessageNotificationVo;
import com.jielin.handling.mapper.mobileapp.MessageNotificationMapper;
import com.jielin.handling.mapper.police.PoliceManMapper;
import com.jielin.handling.page.DataGrid;
import com.jielin.handling.service.mobileapp.MessageNotificationService;
import com.jielin.handling.util.ConstantUtil;
import com.jielin.handling.util.StringUtil;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class MessageNotificationServiceImpl implements MessageNotificationService {
	@Autowired
	MessageNotificationMapper messageNotificationMapper;
	@Autowired
	PoliceManMapper policeManMapper;

	@Override
	public DataGrid findMessageNotification(MessageNotification messageNotification) {
		log.info("查询消息提醒：" + JSONObject.toJSONString(messageNotification));
		String unitCodes = StringUtil.isEmpty(messageNotification.getUnitCode())
				? policeManMapper.findUnitCodeByPoliceCode(messageNotification.getPoliceCode())
				: messageNotification.getUnitCode();
		DataGrid dg = new DataGrid();
		if (StringUtil.isEmpty(unitCodes)) {
			return dg;
		}
		Page<MessageNotificationVo> startPage = PageHelper.startPage(messageNotification.getPageNum(),
				messageNotification.getPageSize());// 使用分页插件方法
		List<MessageNotificationVo> list = messageNotificationMapper.findMessageNotification(unitCodes.split(","));
		dg.setTotal(startPage.getTotal());
		for (MessageNotificationVo mnv : list) {
			if (!StringUtil.isEmpty(mnv.getReads())) {
				String[] readStrings = mnv.getReads().split(",");
				if (readStrings.length > 0
						&& Arrays.asList(readStrings).contains(messageNotification.getPoliceCode())) {// 如果查询来的已读人员警号包含该警员编号则视为已读
					mnv.setIsRead(1);
				}
				mnv.setReads(readStrings.length + "");// 设置已读人数
			} else {
				mnv.setReads("0");
			}
		}
		dg.setRows(list);
		return dg;
	}

	@Override
	public MessageNotificationVo findMessageNotificationById(String id, String policeCode) {
		if (!StringUtil.isEmpty(id) && !StringUtil.isEmpty(policeCode)) {
			messageNotificationMapper.addMessageLog(MessageUser.builder().id(StringUtil.generatorUUID())
					.policeCode(policeCode).msgNotificaId(id).build());// 添加已读记录
			MessageNotificationVo mnv = messageNotificationMapper.findMessageNotificationById(id);
			if (mnv != null) {
				mnv.setIsRead(1);// 设置为已读
			}
			return mnv;
		}
		return null;
	}

	@Override
	public int updateMessageNotificationIsRead(MessageUser messageUser) {
		int flag = -1;
		if (messageUser != null && !StringUtil.isEmpty(messageUser.getMsgNotificaId())
				&& !StringUtil.isEmpty(messageUser.getPoliceCode())) {
			List<MessageUser> list = new ArrayList<>();
			String[] msgNotificaIds = messageUser.getMsgNotificaId().split(",");
			for (String msgNotificaId : msgNotificaIds) {
				list.add(MessageUser.builder().id(StringUtil.generatorUUID()).msgNotificaId(msgNotificaId)
						.policeCode(messageUser.getPoliceCode()).build());
			}
			flag = messageNotificationMapper.addMessageLogs(list);
		}
		return flag;
	}

	@Override
	public List<CaseVo> selectCaseByTime(CaseVo vo) {
		log.info("查询时间：" + JSONObject.toJSONString(vo.getState()));
		List<CaseVo> vos=null;
        if(vo.getState()==0){
			vos=messageNotificationMapper.selectCaseByTime(vo);
			if(vos!=null) {
				for (CaseVo caseVo : vos) {
					String caseCode = caseVo.getCaseCode();
					int count = messageNotificationMapper.getSuspectCount(caseCode);
					caseVo.setSuspectCount(count);
				}
			}
		}
        if(vo.getState()==1){
			vos=messageNotificationMapper.selectCaseByDay(vo);
			if(vos!=null){
			for(CaseVo caseVo:vos){
				String caseCode=caseVo.getCaseCode();
				int count=messageNotificationMapper.getSuspectCount(caseCode);
				caseVo.setSuspectCount(count);
			}
			}
		}
		if(vo.getState()==2){
			vos=messageNotificationMapper.selectCaseByWeek(vo);
			if(vos!=null) {
				for (CaseVo caseVo : vos) {
					String caseCode = caseVo.getCaseCode();
					int count = messageNotificationMapper.getSuspectCount(caseCode);
					caseVo.setSuspectCount(count);
				}
			}
		}
		if(vo.getState()==3){
			vos=messageNotificationMapper.selectCaseByLastMonth(vo);
			if(vos!=null){
			if(vos!=null) {
				for (CaseVo caseVo : vos) {
					String caseCode = caseVo.getCaseCode();
					int count = messageNotificationMapper.getSuspectCount(caseCode);
					caseVo.setSuspectCount(count);
				}
			}
			}
		}

		if(vo.getState()==4){
			vos=messageNotificationMapper.selectCaseByLastYear(vo);
			if(vos!=null){
			for(CaseVo caseVo:vos) {
				String caseCode = caseVo.getCaseCode();
				int count = messageNotificationMapper.getSuspectCount(caseCode);
				caseVo.setSuspectCount(count);
			}
			}
		}
		return vos;
	}

	@Override
	public List<TSuspec> selectAllSusupect(String caseCode) {
		List<TSuspec> list= messageNotificationMapper.selectAllSusupect(caseCode);
		for(TSuspec tSuspec:list){
			if (tSuspec != null) {
				if (StringUtil.isEmpty(tSuspec.getHeadPhoto())) {
					if (tSuspec.getSex() == 1) {
						tSuspec.setHeadPhoto(ConstantUtil.returnManPhoto());
					} else {
						tSuspec.setHeadPhoto(ConstantUtil.returnWoManPhoto());
					}
				} else {
					String returnPath = ConstantUtil.SERVER_URL + tSuspec.getHeadPhoto();
					tSuspec.setHeadPhoto(returnPath);
				}
			}
		}
		return list;
	}

	@Override
	public TSuspecListVo selectHandleCase(String unitcode, String type) {
		TSuspecListVo vo=null;
		if(type.equals("0")){
			vo=messageNotificationMapper.selectDayHandle(unitcode);
		}
		if(type.equals("1")){
			vo=messageNotificationMapper.selectWeekHandle(unitcode);
		}
		if(type.equals("2")){
			vo=messageNotificationMapper.selectMonthHandle(unitcode);
		}
		return vo;
	}

}
